﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using BenefitHelper.Sys;
namespace BenefitHelper.Data.Day
{

    public class AccountDayTradeProductAnalysis
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public int AccountId { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public virtual Account Account { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public int TradeHistoryId { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public virtual TradeHistory TradeHistory { get; set; }
        /// <summary>
        /// 交易品种
        /// </summary>
        public int ProductId { get; set; }
        /// <summary>
        /// 交易品种
        /// </summary>
        public virtual Product Product { get; set; }
        /// <summary>
        /// 多空方向
        /// </summary>
        public int Direction { get; set; }

        /// <summary>
        /// 平均持仓时间
        /// </summary>
        public double AvgPostionTime { get; set; }

        /// <summary>
        /// 最大回撤
        /// </summary>
        public double MaxReturn { get; set; }

        /// <summary>
        /// 超过止损次数
        /// </summary>
        public double OverStopLoss { get; set; }

        /// <summary>
        /// 平进平出单量
        /// </summary>
        public int PingJinPing { get; set; }

        /// <summary>
        /// 收益率
        /// </summary>
        public double Yield { get; set; }
        /// <summary>
        /// 胜率
        /// </summary>
        public double WinRate { get; set; }
        /// <summary>
        /// 风险次数
        /// </summary>
        public int RiskCount { get; set; }
        /// <summary>
        /// 平均单笔盈利
        /// </summary>
        public double AvgBillBenefit { get; set; }

        /// <summary>
        /// 删除某日的所有分析数据
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int tradeHistoryId, DB.DBManager db)
        {
            var query = from t in db.AccountDayTradeProductAnalysis where t.TradeHistoryId == tradeHistoryId select t;
            foreach (AccountDayTradeProductAnalysis d in query)
            {
                db.AccountDayTradeProductAnalysis.Remove(d);
            }
            db.SaveChanges();
        }


        /// <summary>
        /// 保存账号日分析情况
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitAccountProductDayAnalysis(int tradeHistoryId, string pdate, DB.DBManager db)
        {
            List<Data.ServerData.T_Account> serverAccounts = new List<ServerData.T_Account>();
            int connectionStringCount = System.Configuration.ConfigurationManager.ConnectionStrings.Count;
            for (int i = 0; i < connectionStringCount; i++)
            {
                string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[i].ConnectionString;
                if (System.Configuration.ConfigurationManager.ConnectionStrings[i].ProviderName.Equals("System.Data.OracleClient"))
                {
                    Common.ADO.Tools.OracleHelper.connectionString = connectionString;
                    List<BenefitHelper.Data.ServerData.T_Account> accountList = new BenefitHelper.Data.ServerData.T_Account().GetList();
                    foreach (BenefitHelper.Data.ServerData.T_Account _account in accountList)
                    {
                        serverAccounts.Add(_account);
                    }
                }
            }
            foreach (Data.ServerData.T_Account account in serverAccounts)
            {
                Data.ServerData.T_TradeDetail_History tth = new Data.ServerData.T_TradeDetail_History();
                List<string> products = tth.GetAccountDayProduct(pdate, account.Id);
                foreach (string product in products)
                {
                    #region i为0是多，i为1是空
                    for (int i = 0; i < 2; i++)
                    {
                        Data.Day.AccountDayTradeProductAnalysis change = new Data.Day.AccountDayTradeProductAnalysis();
                        Sys.Account _account = new Sys.Account();
                        change.AccountId = _account.GetAccountId(account.LoginAccount);
                        change.TradeHistoryId = tradeHistoryId;
                        change.Direction = i;
                        change.ProductId = new Sys.Product().GetProductId(product, db);
                        change.PingJinPing = tth.GetDayProductPingCount(pdate, account.Id, product, change.Direction.ToString(), db);
                        change.MaxReturn = tth.GetProductMaxReturn(pdate, account.Id, product, change.Direction.ToString(), db);
                        change.AvgPostionTime = -1;
                        change.OverStopLoss = new DayWinOrLoseTick().GetProductOverStopLoss(pdate, account.Id, Config.DefaultValue.OverLostTick, change.ProductId, change.Direction, db);
                        change.WinRate = new DayWinOrLoseTick().GetProductWinRate(pdate, account.Id, change.ProductId, change.Direction, db);
                        double profit = new BenefitHelper.Data.ServerData.T_TradeDetail_History().GetDayProductCloseProfit(pdate, account.Id, product, change.Direction.ToString(), db);
                        double fee = new BenefitHelper.Data.ServerData.T_TradeDetail_History().GetDayProductCommission(pdate, account.Id, product, change.Direction.ToString(), db);
                        if (fee > 0)
                        {
                            change.Yield = (profit - fee) / fee;
                        }
                        else
                        {
                            change.Yield = 0;
                        }
                        change.RiskCount = -1;
                        change.AvgBillBenefit = tth.GetProductAvgBillBenefit(pdate, account.Id, product, change.Direction.ToString(), db);


                        db.AccountDayTradeProductAnalysis.Add(change);
                    }
                    #endregion
                }
            }
            db.SaveChanges();
        }


        public List<AccountDayTradeProductAnalysis> GetAccountDayTradeProductAnalysis(int accountid, int tradeHistoryId, DB.DBManager db)
        {
            List<AccountDayTradeProductAnalysis> instruments = new List<AccountDayTradeProductAnalysis>();
            var query = db.AccountDayTradeProductAnalysis.Where(a => a.AccountId == accountid).Where(a => a.TradeHistoryId == tradeHistoryId);
            if (query.Count() > 0)
            {
                instruments = query.ToList();
            }
            return instruments;
        }
    }
}